package com.mlizhi.widgets.chart;



import com.mlizhi.kingdom.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.view.View;

public class ChartView4PreCircle extends View implements Runnable{

	
	private Canvas canvas;
	private TypedArray array4circle;
	private float width = 0.0f;
	private float height = 0.0f;
	
	/**
	 * 临时变量，模拟时间用的
	 */
	private float t = 0.0f;
	private float average4pre= 0.0f;
	public ChartView4PreCircle(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.array4circle =  context.obtainStyledAttributes(attrs,
				R.styleable.ChartView4PreCircle);
		
	}

	public void startPreProcessArc(){
		Thread currentThread = new Thread(this);
		currentThread.start();
		
		
	}
	
	public ChartView4PreCircle(Context context) {
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		this.canvas = canvas;
		draw4circle();
		draw4preArc();
		invalidate();
	}
	/**
	 * 画圆
	 */
	private void draw4circle(){
		int axis4x = this.getMeasuredWidth();
		int axis4y = this.getMeasuredHeight();
		Paint paint = new Paint();
		
		boolean circle4antiAlias = this.array4circle.getBoolean(R.styleable.ChartView4PreCircle_circle4preAntiAlias, true);
		float circle4strokeWidth = this.array4circle.getDimension(R.styleable.ChartView4PreCircle_circle4preStrokeWidth, 5.0f);
		boolean circle4fill = this.array4circle.getBoolean(R.styleable.ChartView4PreCircle_circle4preFill, false);
		paint.setStrokeWidth(circle4strokeWidth);
		paint.setColor(Color.LTGRAY);
		if(circle4fill == true){
			paint.setStyle(Paint.Style.FILL);
		}else{
			paint.setStyle(Paint.Style.STROKE);
		}
		paint.setAntiAlias(circle4antiAlias);
		
//		float radius = 0.0f;
//		if(axis4y >= axis4x){
//			radius = axis4x*0.98f*0.5f;
//		}else{
//			radius = axis4y*0.98f*0.5f;
//		}
		//与顶端间隔10f，放在circle的边界显示不全
		Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_circle4record);;
//		canvas.drawBitmap(bitmap, null, new RectF(0.5f*axis4x-radius, 0.5f*axis4y-radius,
//				0.5f*axis4x+radius, 0.5f*axis4y+radius), paint);
		width = bitmap.getWidth()*0.5f;
		height = bitmap.getHeight()*0.5f;
		canvas.drawBitmap(bitmap, 0.5f*axis4x-bitmap.getWidth()*0.5f, 0.5f*axis4y-bitmap.getHeight()*0.5f, paint);
	}
	
	/**
	 * 画弧形
	 * 把平均值转换成弧长公式：(360-0)/(0.6-0.2)*(average-0.2)
	 */
	private void draw4preArc( ){
		int axis4x = this.getMeasuredWidth();
		int axis4y = this.getMeasuredHeight();
		
		float pre = this.average4pre * 900f - 180f;
		Paint paint = new Paint();
		float radius = 0.0f;
		if(axis4y >= axis4x){
			radius = axis4x*0.98f*0.5f - 10f;
		}else{
			radius = axis4y*0.98f*0.5f -10f;
		}
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(10.0f);
		paint.setAntiAlias(true);
		paint.setColor(Color.argb( 220, 97, 173, 244));
//		canvas.drawArc(new RectF(0.5f*axis4x-radius, 0.5f*axis4y-radius,
//				0.5f*axis4x+radius, 0.5f*axis4y+radius), 270f, pre*t*0.005f, false, paint);
		
		canvas.drawArc(new RectF(0.5f*axis4x-width*0.95f, 0.5f*axis4y-height*0.95f,
				0.5f*axis4x+width*0.95f, 0.5f*axis4y+height*0.95f), 270f, pre*t*0.005f, false, paint);
		
	}
	
	
	
	@Override
	public void run() {
		 while(t<200.0f) {  
	            try {  
	                Thread.sleep(5);  
	            }  
	            catch(Exception e) {  
	                Thread.currentThread().interrupt();  
	            }  
	            postInvalidate();  
	            t = t+1.0f;
	        }  
		
	}
	
	
	 /**  
     * @return 返回指定笔和指定字符串的长度  
     */  
    public static float getFontlength(Paint paint, String str) {  
        return paint.measureText(str);  
    }  
    /**  
     * @return 返回指定笔的文字高度  
     */  
    public static float getFontHeight(Paint paint)  {    
        FontMetrics fm = paint.getFontMetrics();   
        return fm.descent - fm.ascent;    
    }   
    /**  
     * @return 返回指定笔离文字顶部的基准距离  
     */  
    public static float getFontLeading(Paint paint)  {    
        FontMetrics fm = paint.getFontMetrics();   
        return fm.leading- fm.ascent;    
    }

	public float getAverage4pre() {
		return average4pre;
	}

	public void setAverage4pre(float average4pre) {
		if(average4pre <= 0.20f){
			average4pre = 0.20f;
		}
		this.average4pre = average4pre;
	}
    
}
